import re
from collections import Counter

from pydantic import Field, BaseModel

# eg: 全服排期 合区排期 开服排期 跨合区联盟排期
all_schedule = ['全服', '合区', '开服', '跨合区联盟']


class ScheduleBlockSchema(BaseModel):
    schedule_type: list[str] = Field(..., description='支持的排期类型')
    block_define: str = Field(..., description='block定义')
    block_example: str = Field(..., description='block使用示例')


def match_and_clean_schedule(schedule_type_list: list[str]):
    result = []
    pattern = r'^(.+?)(?:排期)?$'
    for text in schedule_type_list:
        match = re.match(pattern, text)
        if match:
            extracted = match.group(1)
            if extracted not in all_schedule:
                raise Exception(f"不支持的排期类型: {text}")
            print(f"原始文本: {text}, 提取结果: {extracted}")
            result.append(extracted)
    return result


def block_type_tool(
        schedule_type_list: list[str] = Field(..., description='支持的排期类型')
) -> ScheduleBlockSchema:
    if len(schedule_type_list) == 0:
        raise Exception('排期类型列表不能为空')
    # 清洗传入的排期列表
    schedule_type_list = match_and_clean_schedule(schedule_type_list)
    # 根据支持排期数量确定使用的block
    if len(schedule_type_list) == 1:
        return single_block_type(schedule_type_list)
    elif len(schedule_type_list) == 2:
        return double_block_type(schedule_type_list)
    else:
        raise Exception('不支持的排期类型')


def double_block_type(schedule_type_list: list[str]) -> ScheduleBlockSchema:
    # 合区 新服
    if Counter(schedule_type_list) == Counter(['合区', '开服']):
        return ScheduleBlockSchema(
            schedule_type=schedule_type_list,
            block_define="""func GetCrossAndNewServerBaseInfoBlock(key, fileName, sheetName string) excel_reader.Block {
	return excel_reader.Block{
		Key:      key,
		Splitter: excel_reader.NewSplitterByIndex(1, 5),
		Parser: parser.NewBaseInfoParser(
			parser.NewFileNameOption(fileName),
			parser.NewSheetNameOption(sheetName),
			parser.NewMatcherOption(parser.NewNewServerMatcher(), parser.NewCrossServerMatcher()),
		),
		Exporter: exporter.NewBaseInfoExporter(),
	}
}""",
            block_example="""func (e *ExcelParser) getBlocks(sheetName string) []excel_reader.Block {
	return []excel_reader.Block{block.GetCrossAndNewServerBaseInfoBlock("baseInfo", e.fileName, sheetName)}
}""",
        )
    else:
        raise Exception('不支持的排期类型')


def single_block_type(schedule_type_list: list[str]) -> ScheduleBlockSchema:
    schedule_type = schedule_type_list[0]
    if schedule_type == '全服':
        return ScheduleBlockSchema(
            schedule_type=schedule_type_list,
            block_define="""func GetAllServerBaseInfoBlock(key, fileName, sheetName string) excel_reader.Block {
	return excel_reader.Block{
		Key:      key,
		Splitter: excel_reader.NewSplitterByIndex(1, 5),
		Parser: parser.NewBaseInfoParser(
			parser.NewFileNameOption(fileName),
			parser.NewSheetNameOption(sheetName),
			parser.NewMatcherOption(parser.NewAllServerMatcher()),
		),
		Exporter: exporter.NewBaseInfoExporter(),
	}
}""",
            block_example="""func (e *excelParser) getBlocks(sheetName string) []excel_reader.Block {
	return []excel_reader.Block{block.GetAllServerBaseInfoBlock("baseInfo", e.fileName, sheetName)}
}""",
        )
    elif schedule_type == '合区':
        return ScheduleBlockSchema(
            schedule_type=schedule_type_list,
            block_define="""func GetCrossServerBaseInfoBlock(key, fileName, sheetName string) excel_reader.Block {
	return excel_reader.Block{
		Key:      key,
		Splitter: excel_reader.NewSplitterByIndex(1, 5),
		Parser: parser.NewBaseInfoParser(
			parser.NewFileNameOption(fileName),
			parser.NewSheetNameOption(sheetName),
			parser.NewMatcherOption(parser.NewCrossServerMatcher()),
		),
		Exporter: exporter.NewBaseInfoExporter(),
	}
}""",
            block_example="""func (e *ExcelParser) getBlocks(sheetName string) []excel_reader.Block {
	// 支持合区导入
	return []excel_reader.Block{block.GetCrossServerBaseInfoBlock("baseInfo", e.fileName, sheetName)}
}""",
        )
    elif schedule_type == '开服':
        return ScheduleBlockSchema(
            schedule_type=schedule_type_list,
            block_define="""func GetNewServerBaseInfoBlock(key, fileName, sheetName string) excel_reader.Block {
	return excel_reader.Block{
		Key:      key,
		Splitter: excel_reader.NewSplitterByIndex(1, 5),
		Parser: parser.NewBaseInfoParser(
			parser.NewFileNameOption(fileName),
			parser.NewSheetNameOption(sheetName),
			parser.NewMatcherOption(parser.NewNewServerMatcher()),
		),
		Exporter: exporter.NewBaseInfoExporter(),
	}
}""",
            block_example="""func (e *excelParser) getBlocks(sheetName string) []excel_reader.Block {
	return []excel_reader.Block{block.GetNewServerBaseInfoBlock("baseInfo", e.fileName, sheetName)}
}""",
        )
    elif schedule_type == '跨合区联盟':
        return ScheduleBlockSchema(
            schedule_type=schedule_type_list,
            block_define="""func GetNoServerBaseInfoBlock(key, fileName, sheetName string) excel_reader.Block {
	return excel_reader.Block{
		Key:      key,
		Splitter: excel_reader.NewSplitterByIndex(1, 5),
		Parser: parser.NewBaseInfoParser(
			parser.NewFileNameOption(fileName),
			parser.NewSheetNameOption(sheetName),
			parser.NewMatcherOption(parser.NewNoServerMatcher()),
		),
		Exporter: exporter.NewBaseInfoExporter(),
	}
}""",
            block_example="""func (e *ExcelParser) getBlocks(sheetName string) []excel_reader.Block {
	return []excel_reader.Block{
		// 支持排期：跨合区联盟活动
		block.GetNoServerBaseInfoBlock("baseInfo", e.fileName, sheetName),
	}
}""",
        )
    else:
        raise Exception('不支持的排期类型')


if __name__ == '__main__':
    result = block_type_tool(['开服', '合区'])
    print(result)
